#! python3
# -*- coding: utf-8 -*-

from requests_html import HTMLSession
import requests
import os

session = HTMLSession()

#希望优化代码,以及访问分页,构建好函数
# 2018/10/26 现在的问题是每个函数都返回了列表导致数据非常复杂

def pictures_llist(html, jpg_list):

''' 图片链接,返回列表,这个函数放在循环外创建列表 '''

    jpgget = session.get(html)
    jpgurl = jpgget.html.xpath('//p[@align="center"]/img/@src') #图片链接
    jpg_list += jpgurl
    return jpg_list

def pagenext(html):

''' 套图分页链接 '''

    jpgnexturl_lists = []
    jpgget = session.get(html)
    jpgurlnext = jpgget.html.xpath('//ul[@class="pagelist"]/li/a/@href') #下一页链接,不完整地址
    jpgnexturl_lists.extend(jpgurlnext[2:-1])
    # 原有值修改,定义完整链接
    num = 0
    for i in jpgnexturl_lists:
        jpgnexturl_lists[num] = html[:-10] + jpgnexturl_lists[num]
        num += 1
    jpgnexturl_lists.insert(0, html)
    jpgnexturl_list += jpgnexturl_lists
    return jpgnexturl_list

#--------------------------------------------------------
def html_save(html, jpgname):

''' 存储数据 '''

    num = 1 #用来编辑图片名字
    for i in html:
        response = requests.get(i).content
        filenname = path + '/' + jpgname + str(num) + '.jpg' #将图片倒数第二个字符串作为文件名
        print('保存图片：%s' % filenname)
        with open(filenname,'wb') as f:
            f.write(response)
        num += 1

# 爬取图片 html_downloader.py 待编辑
#---------------------------------------------------------

def mkdirjpg():

    '''创建分类文件夹(性感美女,国内美女,高跟丝袜,美女自拍)'''

    path_list = date.html.xpath('//div[@class="w1000"]/a/text()')
    if os.path.exists(path):
        for i in path_list[1:6]:
            paths = path + i
            os.mkdir(paths)
            print('新建文件夹' + paths)
    else:
        os.mkdir(path)
        for i in path_list[1:6]:
            paths = path + i
            os.mkdir(paths)
            print('新建文件夹' + paths)

if __name__ == "__main__":
    urls = 'https://www.169tp.com/'
    date = session.get(urls)
    path = '/mnt/d/home/tupian/' # 预存图片文件夹名称
    mkdirjpg()
    url_xingganmeinv = 'https://www.169tp.com/xingganmeinv/'
    url_wangyouzipai = 'https://www.169tp.com/wangyouzipai/'
    url_gaogensiwa = 'https://www.169tp.com/gaogensiwa/'
    url_xiyangmeinv = 'https://www.169tp.com/xiyangmeinv/'
    url_guoneimeinv = 'https://www.169tp.com/guoneimeinv/'
    # jpgurl = jpgget.html.xpath('//p[@align="center"]/img/@src') #图片链接
    urllist = date.html.xpath('//a[@class="pic"]/@href') #套图链接
    urllistp = date.html.xpath('//a[@class="pic"]/@alt') #套图名称
    pagenext()
    # jpgurlnext = jpgget.html.xpath('//ul[@class="pagelist"]/li/a/@href') #下一页链接,不完整地址

